package org.infodavid.common.ejb.components;

import java.net.URI;
import java.net.URISyntaxException;

import javax.persistence.Column;
import javax.persistence.Embeddable;

import org.infodavid.common.impl.model.UriImpl;

/**
 * The Class UriComponent.
 */
@Embeddable
public class UriComponent extends UriImpl {

  /** serialVersionUID. */
  private static final long serialVersionUID = 584923281312002874L;

  /**
   * The Constructor.
   * @throws Exception the exception
   */
  protected UriComponent() throws Exception {
    super();
  }

  /**
   * Gets the string value.
   * @return the string value
   */
  @Column(name = VALUE, nullable = false, length = 255)
  protected String getStringValue() {
    final URI uri = super.getValue();

    return uri == null ? null : uri.toString();
  }

  /**
   * Sets the string value.
   * @param arg the string value
   */
  protected void setStringValue(final String arg) {
    URI uri;

    if (arg == null) {
      uri = null;
    }
    else {
      try {
        uri = new URI(arg);
      }
      catch (final URISyntaxException e) {
        LOGGER.warn("URI is not valid: " + arg, e);

        uri = null;
      }
    }

    setValue(uri);
  }
}
